package com.ys.web.controller;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.ys.WebConstants;
import com.ys.extend.common.exception.BusinessRuntimeException;
import com.ys.extend.common.model.AjaxJson;
import com.ys.web.entity.CustomerTarget;
import com.ys.web.entity.IndustryGoal;
import com.ys.web.pojo.CompanyInfo;
import com.ys.web.pojo.IndustryGoalInfo;
import com.ys.web.pojo.IndustryMsgInfo;
import com.ys.web.pojo.SystemPermissonInfo;
import com.ys.web.pojo.TotalNotGoal;
import com.ys.web.security.SystemPermissionUtils;
import com.ys.web.service.CompanyService;
import com.ys.web.service.IndustryGoalService;
import com.ys.web.service.IndustryService;

/**
 * 行业目标值管理--sem经理使用
 * 
 * @author 卓小建
 *
 */
@Scope("prototype")
@Controller
@RequestMapping("/industryGoalController")
public class IndustryGoalController {

	@Autowired
	private IndustryService industryService;

	@Autowired
	private IndustryGoalService industryGoalService;

	@Autowired
	private CompanyService companyService;
	public SimpleDateFormat sdd = new SimpleDateFormat("yyyy-MM-dd");
	DecimalFormat dfd = new DecimalFormat("######0.00");

	private String message;

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	@RequestMapping(params = "industryGoal")
	public ModelAndView showIndustry(HttpServletRequest request) {

		return new ModelAndView("industry/industryGoal");
	}

	@RequestMapping(params = "getALLIndustryByPage")
	@ResponseBody
	public List<IndustryGoalInfo> getALLIndustryByPage(Integer page) {
		Integer currentPage = 1;
		if (page != null) {
			currentPage = page;
		}

		List<IndustryMsgInfo> list1 = industryService.getALLIndustryByPage(currentPage);
		List<IndustryGoalInfo> list = new ArrayList<>();
		for (IndustryMsgInfo indu : list1) {
			IndustryGoalInfo ig = new IndustryGoalInfo();
			IndustryGoal idu = null;
			try {
				idu = industryGoalService.getTById(IndustryGoal.class, indu.getIndustryId());
			} catch (Exception e) {
				e.printStackTrace();
			}
			if (idu == null) {
				ig.setId(indu.getIndustryId());
				ig.setIndustryName(indu.getIndustryName());
				ig.setAvgConsultGoal(-2.0);
				ig.setAvgConversionGoal(-2.0);
			} else {
				ig.setId(indu.getIndustryId());
				ig.setIndustryName(indu.getIndustryName());
				ig.setAvgConsultGoal(idu.getAvgConsultGoal());
				ig.setAvgConversionGoal(idu.getAvgConversionGoal());
			}
			list.add(ig);
		}
		return list;
	}

	@RequestMapping(params = "doAddOrUpdate")
	@ResponseBody
	public AjaxJson doAddOrUpdate(@RequestBody IndustryGoalInfo industryGoalInfo) {
		AjaxJson j = new AjaxJson();
		message = "success";
		try {
			// 执行新增或者更新
			IndustryGoal induu = industryGoalService.getTById(IndustryGoal.class, industryGoalInfo.getId());
			if (induu == null) {
				IndustryGoal ig = new IndustryGoal();
				ig.setId(industryGoalInfo.getId());
				ig.setAvgConsultGoal(industryGoalInfo.getAvgConsultGoal());
				ig.setAvgConversionGoal(industryGoalInfo.getAvgConversionGoal());
				industryGoalService.save(ig);
			} else {
				induu.setAvgConsultGoal(industryGoalInfo.getAvgConsultGoal());
				induu.setAvgConversionGoal(industryGoalInfo.getAvgConversionGoal());
				industryGoalService.saveOrUpdate(induu);
			}

		} catch (Exception e) {
			e.printStackTrace();
			j.setSuccess(false);
			message = "fail";
			throw new BusinessRuntimeException(e.getMessage());
		}

		j.setMsg(message);
		return j;
	}

	/**
	 * 客户指标
	 */
	@RequestMapping(params = "companyGoal")
	public ModelAndView companyGoal(HttpServletRequest request) {

		return new ModelAndView("company/companyGoal");
	}

	@RequestMapping(params = "getCompanys")
	@ResponseBody
	public List<CustomerTarget> getCompanyGoalPage(Integer page, HttpSession session, HttpServletRequest request) {
		String username = (String) session.getAttribute("username");
		Integer userId = (Integer) session.getAttribute("userId");
		String ptime = request.getParameter("ptime");
		String endtime = request.getParameter("endtime");
		List<CustomerTarget> customerTarget = new ArrayList<CustomerTarget>();
		List<CompanyInfo> companyInfos = new ArrayList<CompanyInfo>();
		if (username != null && userId != null) {
			SystemPermissonInfo systemPermissonInfo = SystemPermissionUtils.getUserPermission(username);
			Integer infopage = 1;
			if (page != null) {
				infopage = page;
			}

			try {
				if (systemPermissonInfo.getRoleId().contains(WebConstants.SUPER_USER)
						|| systemPermissonInfo.getRoleId().contains(WebConstants.SEM_MANAGER_USER)) {
					companyInfos = companyService.getRemoteCompanyBySemUserId_baidu(null, infopage);
					customerTarget = industryGoalService.getCustomerGoal(companyInfos, ptime, endtime);

				} else {
					companyInfos = companyService.getRemoteCompanyBySemUserId_baidu(userId, infopage);
					customerTarget = industryGoalService.getCustomerGoal(companyInfos, ptime, endtime);
				}

			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}
		return customerTarget;

	}

	/**
	 * 未达标客户指标
	 */
	@RequestMapping(params = "companyNotGoal")
	public ModelAndView companyNotGoal(HttpServletRequest request) {

		return new ModelAndView("company/companyNotGoal");
	}

	@RequestMapping(params = "getNotCompanys")
	@ResponseBody
	public List<CustomerTarget> getCompanyNotGoalPage(Integer page, HttpSession session, HttpServletRequest request) {
		String username = (String) session.getAttribute("username");

		Integer userId = (Integer) session.getAttribute("userId");
		String ptime = request.getParameter("ptime");
		String endtime = request.getParameter("endtime");

		List<CustomerTarget> customerTarget = new ArrayList<CustomerTarget>();

		List<CompanyInfo> companyInfos = new ArrayList<CompanyInfo>();
		if (username != null && userId != null) {

			SystemPermissonInfo systemPermissonInfo = SystemPermissionUtils.getUserPermission(username);
			Integer infopage = 1;
			if (page != null) {
				infopage = page;
			}
			try {
				if (systemPermissonInfo.getRoleId().contains(WebConstants.SUPER_USER)
						|| systemPermissonInfo.getRoleId().contains(WebConstants.SEM_MANAGER_USER)) {
					// companyInfos=companyService.getCompanyBySemUserId(null,
					// infopage);
					companyInfos = companyService.getRemoteCompanyBySemUserId_baidu(null, infopage);
					customerTarget = industryGoalService.getCustomerNotGoal(companyInfos, ptime, endtime);

				} else {
					// companyInfos =
					// companyService.getCompanyBySemUserId(userId, infopage);
					companyInfos = companyService.getRemoteCompanyBySemUserId_baidu(userId, infopage);
					customerTarget = industryGoalService.getCustomerNotGoal(companyInfos, ptime, endtime);
				}

			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}
		return customerTarget;
	}

	public static String getBeforeSimpleDate() {

		SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
		Calendar ca_before = Calendar.getInstance();
		ca_before.set(Calendar.DATE, ca_before.get(Calendar.DATE) - 1);
		return sd.format(ca_before.getTime());
	}

	/**
	 * 未达标客户统计
	 */
	@RequestMapping(params = "companyTotalNotGoal")
	public ModelAndView companyTotalNotGoal(HttpServletRequest request) {

		return new ModelAndView("company/companyTotalNotGoal");
	}

	@RequestMapping(params = "getTotalNotByPage")
	@ResponseBody
	public List<TotalNotGoal> getTotalNotByPage(Integer page, HttpSession session, HttpServletRequest request) {

		String username = (String) session.getAttribute("username");

		Integer userId = (Integer) session.getAttribute("userId");
		String ptime = request.getParameter("ptime");
		String endtime = request.getParameter("endtime");

		List<TotalNotGoal> tng = new ArrayList<>();
		if (username != null && userId != null) {

			SystemPermissonInfo systemPermissonInfo = SystemPermissionUtils.getUserPermission(username);
			Integer infopage = 1;
			if (page != null) {
				infopage = page;
			}

			try {
				if (systemPermissonInfo.getRoleId().contains(WebConstants.SUPER_USER)
						|| systemPermissonInfo.getRoleId().contains(WebConstants.SEM_MANAGER_USER)) {
					// companyInfos=companyService.getCompanyBySemUserId(null,
					// infopage);
					tng = industryGoalService.getCustomerTotalNotGoal(ptime, endtime, infopage);
				}

			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}
		return tng;
	}

	@RequestMapping(params = "getsemCompanys")
	@ResponseBody
	public List<CustomerTarget> getSemNotGoalPage(Integer page, HttpSession session, HttpServletRequest request) {
		Integer userId = Integer.parseInt(request.getParameter("userId"));
		String ptime = request.getParameter("ptime");
		String endtime = request.getParameter("endtime");
		List<CustomerTarget> customerTarger = null;
		List<CompanyInfo> companyInfos = new ArrayList<CompanyInfo>();
		Integer infopage = 1;
		if (page != null) {
			infopage = page;
		}
		companyInfos = companyService.getRemoteCompanyBySemUserId_baidu(userId, infopage);
		customerTarger = industryGoalService.getCustomerSemNotGoal(companyInfos, ptime, endtime, userId);
		return customerTarger;
	}

}
